# Name: JoinRoadCostDistanceGrid.py - MCBS 10/20/2014
# Description: Joins road cost distance transportation cost from raster to Grid and exports data in .dbf.
# The raster RoadCostDistanceWGS is generated by see Data/TransCost/RoadTransCostRaster.py. It should be added to Data/GridBuild/Rasters.gdb

# import system module
import arcpy
from arcpy import env
from arcpy.sa import *

# Set workspace environment:
#env.workspace = "Rasters.gdb"
env.workspace = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\Rasters.gdb"
env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")

# Folder to store tables:
PathForTables = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\Tables"

# Input grid:
GridName           = "G1kmMiRWGS"

# Input rasters:
InCostDistance      = "RoadCostDistanceWGS"

# Output grids name:
OutCostDistance     = "G1kmMiRWGSRoadCostDistance"

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

print " "
print " Extracting cost distance to grid ..."
# Execute ExtractValuesToPoints for Elevation:
ExtractValuesToPoints(GridName, InCostDistance, OutCostDistance,
                      "INTERPOLATE", "VALUE_ONLY")
print OutCostDistance + " created."
print " Writing table ... "
TableCostName    = OutCostDistance + ".dbf"
arcpy.TableToTable_conversion(OutCostDistance, PathForTables, TableCostName)
print TableCostName + " written."
